package com.xiaotu.spider.maoyan.pipeline;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanBoxofficeModel;
import com.xiaotu.common.model.MaoYanMaterielsModel;
import com.xiaotu.common.model.MaoYanMovieBoxDetailModel;
import com.xiaotu.common.model.MaoYanMovieCityModel;
import com.xiaotu.common.model.MaoYanMovieMarketModel;
import com.xiaotu.common.model.MaoYanMoviePeopleModel;
import com.xiaotu.common.model.MaoYanMovieScoreRelateModel;
import com.xiaotu.common.model.MaoYanMovieShadowModel;
import com.xiaotu.common.model.MaoYanMovieSplitModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.APipeline;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

/***
 * 
 * @author Mr.zhang
 *
 */
@Component("MaoYanMovieRelatedDataPipeline")
public class MaoYanMovieRelatedDataPipeline extends APipeline {

	@Override
	protected void doProcess(ResultItems resultItems, Task task) {
		
		List<MaoYanBoxofficeModel> list = resultItems.get(MaoYanBoxofficeModel.class.getName());
        if (list != null && !list.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_BOXOFFICE_YYYYMMDD);
            for (MaoYanBoxofficeModel maoYanBoxofficeModel : list) {
            	cacheHandler.hSet(key, maoYanBoxofficeModel.getRedisKey(), maoYanBoxofficeModel);
            	cacheHandler.setCacheTimeout(key);
    		}
        }
		
		List<MaoYanMovieCityModel> cityList = resultItems.get(MaoYanMovieCityModel.class.getName());
        if (cityList != null && !cityList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_AREA_YYYYMMDD);
            cacheHandler.hMSet(key, cityList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getAreaName()+"_"+ mov.getId(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }
        
        List<MaoYanMovieShadowModel> companyList = resultItems.get(MaoYanMovieShadowModel.class.getName());
        if (companyList != null && !companyList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_COMPANY_YYYYMMDD);
            cacheHandler.hMSet(key, companyList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getAreaName() +"_"+ mov.getId(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }
        MaoYanMoviePeopleModel people = resultItems.get(MaoYanMoviePeopleModel.class.getName());
        if (people != null)
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_PEOPLE_YYYYMMDD);
            cacheHandler.hSet(key, people.getDataId()+"_"+people.getId(), people);
            cacheHandler.setCacheTimeout(key);
        }
        
        MaoYanMovieSplitModel split = resultItems.get(MaoYanMovieSplitModel.class.getName());
        if (split != null)
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_SPLIT_YYYYMMDD);
            cacheHandler.hSet(key, split.getDataId() +"_"+split.getId(), split);
            cacheHandler.setCacheTimeout(key);
        }
        
        List<MaoYanMovieMarketModel> marketList = resultItems.get(MaoYanMovieMarketModel.class.getName());
        if (marketList != null && !marketList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_MARKET_YYYYMMDD);
            cacheHandler.hMSet(key, marketList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getDate() + SepratorUtil
                    .SEP_UNDERLINE + mov.getTitle()+"_"+ mov.getId(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }
		
        
        MaoYanMovieScoreRelateModel movieScore = resultItems.get(MaoYanMovieScoreRelateModel.class.getName());
        if (movieScore != null)
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_SCORE_RELATE_YYYYMMDD);
            cacheHandler.hSet(key, movieScore.getDataid()+"_"+ movieScore.getId(), movieScore);
            cacheHandler.setCacheTimeout(key);
        }
        
        List<MaoYanMovieBoxDetailModel> boxDetailList = resultItems.get(MaoYanMovieBoxDetailModel.class.getName());
        if (boxDetailList != null && !boxDetailList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_BOX_DETAIL_YYYYMMDD);
            cacheHandler.hMSet(key, boxDetailList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataid() + SepratorUtil.SEP_UNDERLINE + mov.getPlayDate() + SepratorUtil
                    .SEP_UNDERLINE + mov.getBroadCastType() +"_"+ mov.getId(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }
        
        MaoYanMaterielsModel materielsModel = resultItems.get(MaoYanMaterielsModel.class.getName());
        if (materielsModel != null)
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_MATERIELS_YYYYMMDD);
            cacheHandler.hSet(key, materielsModel.getDataid()+"_"+materielsModel.getId(), materielsModel);
            cacheHandler.setCacheTimeout(key);
        }
        
        
	}
	
}
